home comics writing pictures archive about

Lang.cpp

Language: C++
Last Modified: 2022-08-18 12:03:01 AM UTC
File Size: 5659 bytes
http://www.penguinstew.ca/example/CodeFormater/Lang.cpp
#include "Lang.h"
#include <sstream>
#include "PhpHelper.h"
#define LANG_TYPE_ATTRIBUTE "type"
#define LANG_COLOUR_ATTRIBUTE "colour"
#define LANG_START_ATTRIBUTE "start"
#define LANG_END_ATTRIBUTE "end"
#define LANG_STARTTYPES_ELEMENT "startTypeIds"
#define LANG_STARTTYPE_ELEMENT "startType"
#define LANG_ENDTYPES_ELEMENT "endTypeIds"
#define LANG_ENDTYPE_ELEMENT "endType"
const std::string Lang::LANG_PATTERN_BASE = "~base";
const std::string Lang::LANG_PATTERN_INLANG = "~inLang";
Lang::Lang()
{
type = "";
colour = "";
startSequence = Lang::LANG_PATTERN_BASE;
endSequence = "";
startIds = std::vector<TypeIdPair>();
endIds = std::vector<TypeIdPair>();
baseType = true;
colourInLang = false;
}
Lang::Lang(std::string startType)
{
type = startType;
colour = "";
startSequence = Lang::LANG_PATTERN_BASE;
endSequence = "";
startIds = std::vector<TypeIdPair>();
endIds = std::vector<TypeIdPair>();
baseType = true;
colourInLang = false;
}
Lang::Lang(xmlNodePtr xmlNode)
{
type = "";
colour = "";
startSequence = "";
endSequence = "";
startIds = std::vector<TypeIdPair>();
endIds = std::vector<TypeIdPair>();
baseType = false;
colourInLang = false;
xmlChar* xmlString;
xmlNodePtr xmlChild = NULL;
xmlNodePtr xmlSubChild = NULL;
xmlString = xmlGetProp(xmlNode, xmlCharStrdup(LANG_TYPE_ATTRIBUTE));
if (xmlString != NULL)
{
type = std::string((char*)xmlString);
}
xmlString = xmlGetProp(xmlNode, xmlCharStrdup(LANG_COLOUR_ATTRIBUTE));
if (xmlString != NULL)
{
colour = std::string((char*)xmlString);
}
colourInLang = colour.compare(Lang::LANG_PATTERN_INLANG) == 0;
xmlString = xmlGetProp(xmlNode, xmlCharStrdup(LANG_START_ATTRIBUTE));
if (xmlString != NULL)
{
startSequence = std::string((char*)xmlString);
}
baseType = startSequence.compare(Lang::LANG_PATTERN_BASE) == 0;
xmlString = xmlGetProp(xmlNode, xmlCharStrdup(LANG_END_ATTRIBUTE));
if (xmlString != NULL)
{
endSequence = std::string((char*)xmlString);
}
for (xmlChild = xmlNode->children; xmlChild != NULL; xmlChild = xmlChild->next)
{
if (xmlChild->type != XML_ELEMENT_NODE) {
continue;
}
if (xmlStrEqual(xmlChild->name, xmlCharStrdup(LANG_STARTTYPES_ELEMENT)))
{
for (xmlSubChild = xmlChild->children; xmlSubChild != NULL; xmlSubChild = xmlSubChild->next)
{
if (xmlChild->type != XML_ELEMENT_NODE) {
continue;
}
if (xmlStrEqual(xmlSubChild->name, xmlCharStrdup(LANG_STARTTYPE_ELEMENT)))
{
TypeIdPair typePair = TypeIdPair(xmlSubChild);
startIds.push_back(typePair);
}
}
}
if (xmlStrEqual(xmlChild->name, xmlCharStrdup(LANG_ENDTYPES_ELEMENT)))
{
for (xmlSubChild = xmlChild->children; xmlSubChild != NULL; xmlSubChild = xmlSubChild->next)
{
if (xmlChild->type != XML_ELEMENT_NODE) {
continue;
}
if (xmlStrEqual(xmlSubChild->name, xmlCharStrdup(LANG_ENDTYPE_ELEMENT)))
{
TypeIdPair typePair = TypeIdPair(xmlSubChild);
endIds.push_back(typePair);
}
}
}
}
}
std::string Lang::GetType()
{
return type;
}
std::string Lang::GetColour()
{
return colour;
}
std::string Lang::GetStartSequence()
{
return startSequence;
}
std::string Lang::GetEndSequence()
{
return endSequence;
}
std::vector<TypeIdPair> Lang::GetStartIds()
{
return startIds;
}
std::vector<TypeIdPair> Lang::GetEndIds()
{
return endIds;
}
bool Lang::IsBaseType()
{
return baseType;
}
bool Lang::IsColourInLang()
{
return colourInLang;
}
bool Lang::IsStart(std::string line, int pos, TypeIdPair testType)
{
if (startSequence.compare(line.substr(pos, startSequence.length())) != 0)
{
return false;
}
for (unsigned int i = 0; i < startIds.size(); i++)
{
TypeIdPair ti = startIds.at(i);
if (ti.IsMatch(testType))
{
return true;
}
}
return false;
}
bool Lang::IsEnd(std::string line, int pos, TypeIdPair testType)
{
if (endSequence.compare(line.substr(pos, endSequence.length())) != 0)
{
return false;
}
for (unsigned int i = 0; i < endIds.size(); i++)
{
TypeIdPair ti = endIds.at(i);
if (ti.IsMatch(testType))
{
return true;
}
}
return false;
}
int Lang::PrintStart(std::stringstream& lineStream)
{
if (!colourInLang)
{
//print start
lineStream << "<span class=\"" << colour << "\">";
std::string start = PhpHelper::htmlspecialchars(startSequence);
lineStream << start << "</span>";
//Update index
return startSequence.length();
}
return 0;
}
int Lang::PrintEnd(std::stringstream& lineStream)
{
if (!colourInLang)
{
//print end
lineStream << "<span class=\"" << colour << "\">";
std::string start = PhpHelper::htmlspecialchars(endSequence);
lineStream << start << "</span>";
//Update index
return endSequence.length();
}
return 0;
}
std::string Lang::ToString()
{
std::stringstream stream;
stream << "Lang: ";
stream << "Type: " << this->type << ", ";
stream << "Colour: " << this->colour << ", ";
stream << "Start Sequence: " << this->startSequence << ", ";
stream << "End Sequence: " << this->endSequence << std::endl;
stream << "--- Start IDs ---" << std::endl;
for (unsigned int i = 0; i < startIds.size(); i++)
{
TypeIdPair pair = startIds.at(i);
stream << pair.ToString() << std::endl;
}
stream << "--- End IDs ---" << std::endl;
for (unsigned int i = 0; i < endIds.size(); i++)
{
TypeIdPair pair = endIds.at(i);
stream << pair.ToString() << std::endl;
}
return stream.str();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256